#include "TreeWidget_DisplayScriptList.h"

#include <qlist.h>
#include <qmessagebox.h>

#include "ScriptInstruction.h"
#include "ScriptInstructionFactory.h"
#include "ScriptRunner.h"

TreeWidget_DisplayScriptList::TreeWidget_DisplayScriptList(QWidget *parent)
	: QTreeWidget(parent)
{
}

TreeWidget_DisplayScriptList::~TreeWidget_DisplayScriptList()
{
	for (int i = 0; i < topLevelItemCount(); ++i)
	{
		circularDelete(takeTopLevelItem(i));
	}
}
void TreeWidget_DisplayScriptList::circularDelete(QTreeWidgetItem* parent)
{
	for (int i = 0; i < parent->childCount(); ++i)
	{
		circularDelete(parent->child(i));
	}
	delete parent;
}

void TreeWidget_DisplayScriptList::slot_showCurrentInstruction(int codeBlockIndex, int instructionIndex)
{
	QBrush brush;
	brush.setStyle(Qt::SolidPattern);
	if (lastHighLightNode != Q_NULLPTR)
	{
		brush.setColor(Qt::white);
		lastHighLightNode->setBackground(0, brush);
	}
	brush.setColor(Qt::green);
	QTreeWidgetItem* currentItem = Q_NULLPTR;
	if (codeBlockIndex == 0)
	{
		currentItem = topLevelItem(instructionIndex);
	}
	else
	{
		currentItem = codeBlockItems.at(codeBlockIndex - 1)->child(instructionIndex);
	}
	currentItem->setBackground(0, brush);
	setCurrentItem(currentItem);
	lastHighLightNode = currentItem;
}

void TreeWidget_DisplayScriptList::slot_clearCurrentInstruction()
{
	QBrush brush;
	brush.setStyle(Qt::SolidPattern);
	if (lastHighLightNode != Q_NULLPTR) {
		brush.setColor(Qt::white);
		lastHighLightNode->setBackground(0, brush);
	}
	setCurrentItem(Q_NULLPTR);
}

void TreeWidget_DisplayScriptList::slot_updateScriptList(QVector<QVector<ScriptInstruction*>> scriptList)
{
	clear();
	codeBlockItems.clear();//清空记录代码块的列表
	lastHighLightNode = Q_NULLPTR;
	if (scriptList.isEmpty()) {//整个代码块都是空的就不用继续解析了
		return;
	}
	bool isOk = false;
	QVector<QTreeWidgetItem*> treeWidgetItemList = ScriptInstructionFactory::scriptListToTreeWidgetItemList(scriptList, &isOk);
	if (!isOk)
	{
		QMessageBox(QMessageBox::Critical, QStringLiteral("错误"), QStringLiteral("显示当前脚本的过程中，格式转换出错，显示当前脚本失败"));
		return;
	}
	addTopLevelItems(QList<QTreeWidgetItem*>::fromVector(treeWidgetItemList));
	codeBlockItems.append(ScriptInstructionFactory::getParentTreeWidgetItemList(treeWidgetItemList));
}

ScriptRunner* TreeWidget_DisplayScriptList::getScriptRunner()
{
	return scriptRunner;
}

void TreeWidget_DisplayScriptList::setScriptRunner(ScriptRunner* scriptRunner)
{
	disconnect(this->scriptRunner, &ScriptRunner::signal_scriptListChanged, this, &TreeWidget_DisplayScriptList::slot_updateScriptList);
	disconnect(this->scriptRunner, &ScriptRunner::signal_currentInstruction, this, &TreeWidget_DisplayScriptList::slot_showCurrentInstruction);
	disconnect(this->scriptRunner, &ScriptRunner::signal_scriptStoped, this, &TreeWidget_DisplayScriptList::slot_clearCurrentInstruction);
	this->scriptRunner = scriptRunner;
	connect(this->scriptRunner, &ScriptRunner::signal_scriptListChanged, this, &TreeWidget_DisplayScriptList::slot_updateScriptList);
	connect(this->scriptRunner, &ScriptRunner::signal_currentInstruction, this, &TreeWidget_DisplayScriptList::slot_showCurrentInstruction);
	connect(this->scriptRunner, &ScriptRunner::signal_scriptStoped, this, &TreeWidget_DisplayScriptList::slot_clearCurrentInstruction);
}
